package com.hzeport.bgs22emduty.config.interptor;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletAttributes {
    private static ThreadLocal<HttpServletRequest> requestContext = new ThreadLocal();
    private static ThreadLocal<HttpServletResponse> responseContext = new ThreadLocal();

    public ServletAttributes() {
    }

    public static void setResponse(HttpServletResponse response) {
        responseContext.set(response);
    }

    public static void setRequest(HttpServletRequest request) {
        requestContext.set(request);
    }

    public static HttpServletRequest getRequest() {
        HttpServletRequest request = (HttpServletRequest)requestContext.get();
        if (null == request) {
            request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        }
        return request;
    }

    public static HttpServletResponse getResponse() {
        return (HttpServletResponse)responseContext.get();
    }
}
